package cn.wangyu.msblogadmin.admin.controller;


import cn.wangyu.msblogadmin.admin.dto.AdminDto;
import cn.wangyu.msblogadmin.admin.entity.SysAdmin;
import cn.wangyu.msblogadmin.admin.service.ISysAdminService;
import cn.wangyu.msblogadmin.admin.vo.AdminVo;
import cn.wangyu.msblogadmin.common.base.BaseController;
import cn.wangyu.msblogadmin.common.result.ResultModel;
import cn.wangyu.msblogadmin.common.utils.ResultUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author wangyu
 * @since 2021-09-04
 */
@RestController
@RequestMapping("/admin")
public class SysAdminController extends BaseController {

    @Autowired
    private ISysAdminService adminService;

    @ApiOperation(value = "新增管理员",notes = "新增管理员用户")
    @PostMapping(value = "/")
    public ResultModel<SysAdmin> save(@Validated(AdminVo.AdminInsert.class) @RequestBody AdminVo adminVo){
        /*SysAdmin admin = AdminMapstruct.INSTANCE.adminVoToAdmin(adminVo);
        admin.setRoleIds(StringUtils.join(adminVo.getRoleArray(),","));
        adminService.save(admin);
        */
        return ResultUtil.success();
    }

    @ApiOperation(value = "修改管理员信息",notes = "修改管理员信息")
    @PatchMapping(value = "/")
    public ResultModel<SysAdmin> update ( @Validated(AdminVo.AdminUpdate.class) @RequestBody AdminVo adminVo){
        /*SysAdmin admin = AdminMapstruct.INSTANCE.adminVoToAdmin(adminVo);
        admin.setRoleIds(StringUtils.join(adminVo.getRoleArray(),","));
        adminService.updateById(admin)
        */
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除管理员",notes = "根据id管理员信息")
    @DeleteMapping("/{id}")
    public ResultModel<Boolean> remove (@NotNull(message = "id不能为空")@PathVariable("id")Long id){
        boolean b = adminService.removeById(id);
        return ResultUtil.success(b);
    }

    @ApiOperation(value = "分页查询",notes = "条件分页查询管理员数据")
    @GetMapping("/findPages")
    public ResultModel<IPage<SysAdmin>> findPages(AdminDto query){
        IPage<SysAdmin> pages = adminService.findPages(query);
        return this.success(pages);
    }

    @ApiOperation(value = "查询管理员账户详情",notes = "根据ID查询管理员账户详情")
    @GetMapping("/{id}")
    public ResultModel<SysAdmin> getById(@NotNull(message = "id不能为空")@PathVariable Long id){
        SysAdmin admin = adminService.getById(id);
        return this.success(admin);
    }

    @ApiOperation(value = "查询管理员信息",notes = "根据id管理员信息")
    @GetMapping("/info")
    public ResultModel<SysAdmin> info (Long id){
        Long id1 = 1386704997729652737L;
        SysAdmin admin = adminService.getById(id1);
        return ResultUtil.success(admin);
    }

    @ApiOperation(value = "根据用户名查询")
    @GetMapping("/username")
    public ResultModel<SysAdmin> getByUsername(String username){
        SysAdmin admin = adminService.getByUsername(username);
        return success(admin);
    }
}
